package com.czr.service.collect;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.czr.entity.Collect;
import com.czr.entity.CollectCategory;
import com.czr.mapper.CollectMapper;
import com.czr.service.collectcategory.CollectCategoryService;
import com.czr.vo.CollectVo;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Optional;
import java.util.stream.Collectors;

/**
 * Description:
 * Author: czr
 * Version: 1.0
 * Create Date Time: 2021/12/22 17:11.
 */
@Service
@Slf4j
public class CollectServiceImpl extends ServiceImpl<CollectMapper, Collect> implements CollectService {

    @Autowired
    private CollectCategoryService collectCategoryService;

    // concat拼接 split分割方式
    /*@Override
    public List<CollectVo> findList() {
        log.info("--->CollectServiceImpl");
        // 查询分类数据
        List<CollectCategory> collectCategorylist = collectCategoryService.list();

        // 1：查询表中所有的收藏夹数据
        LambdaQueryWrapper<Collect> queryWrapper = new LambdaQueryWrapper();
        queryWrapper.eq(Collect::getStatus, 1);
        List<Collect> collects = this.list(queryWrapper);
        // 2：根据分类id进行排序
        Map<String, List<Collect>> collectMap = collects.stream().collect(Collectors.groupingBy(collect -> collect.getCategoryTitle().concat(",").concat(collect.getCategoryId() + "")));

        List<CollectVo> collectVoList = new ArrayList<>();
        // 3：根据分类id进行收藏夹查询
        collectMap.entrySet().forEach(entry -> {
            CollectVo collectVo = new CollectVo();

            String[] split = entry.getKey().split(",");

            collectVo.setCategoryId(Integer.parseInt(split[1]));
            collectVo.setCategoryTitle(split[0]);

            collectVo.setCollectList(entry.getValue());

            collectVoList.add(collectVo);

        });
        // 4：返回即可

        return collectVoList;
    }*/

    
    @Override
    public List<CollectVo> findList() {
        log.info("--->CollectServiceImpl--entry.getValue().get(0).getCategoryTitle()");

        // 1：查询表中所有的收藏夹数据
        LambdaQueryWrapper<Collect> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(Collect::getStatus, 1);
        List<Collect> collects = this.list(queryWrapper);
        // 2：根据分类id进行排序
        Map<Integer, List<Collect>> collectMap = collects.stream().collect(Collectors.groupingBy(Collect::getCategoryId)); // 只要分组排序绝对是有数据的

        List<CollectVo> collectVoList = new ArrayList<>();
        // 3：根据分类id进行收藏夹查询
        collectMap.forEach((key, value) -> {
            CollectVo collectVo = new CollectVo();

            // 放入collectVo封装返回数据
            collectVo.setCategoryId(key);
            collectVo.setCategoryTitle(value.get(0).getCategoryTitle()); // 能分组排序出来就会有数据，放心不能报空指针
            collectVo.setCollectList(value);

            collectVoList.add(collectVo);

        });
        // 4：返回即可

        return collectVoList;
    }

}
